﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Wuliu.SDK.Core
{
    public class HttpRequest
    {
        public static async Task<T> GetAsync<T>(string url)
        {
            var httpClient = new HttpClient();

            string response = await httpClient.GetStringAsync(url);

            T t = JsonConvert.DeserializeObject<T>(response);

            return t;
        }

        public static async Task<string> GetStringAsync(string url)
        {
            var httpClient = new HttpClient();

            string response = await httpClient.GetStringAsync(url);

            return response;
        }

        public static async Task<T> PostAsync<T>(string url,string body =null,string mediaTyp = "application/json")
        {
            StringContent httpContent=null;
            if(!string.IsNullOrEmpty(body))
            {
                httpContent = new StringContent(body, Encoding.UTF8, mediaTyp);
            }

            var httpClient = new HttpClient();
           
            var response = await httpClient.PostAsync(url, httpContent);

            string result = await response.Content.ReadAsStringAsync();

            T t = JsonConvert.DeserializeObject<T>(result);

            return t;
        }

        
    }
}
